package GeneticAlgorithm_Five;

import java.util.Arrays;

public class Individual {
    private int[] chromosome;
    private double fitness = -1;

    // 已验
    public Individual(int[] chromosome) {
        this.chromosome = chromosome;
    }

    // 已验
    public int[] getChromosome() {
        return this.chromosome;
    }

    // 已验
    public int getChromosomeLength() {
        return this.chromosome.length;
    }

    // 已验
    public void setGene(int offset, int gene) {
        this.chromosome[offset] = gene;
    }

    // 已验
    public int getGene(int offset) {
        return this.chromosome[offset];
    }

    // 已验
    public void setFitness(double fitness) {
        this.fitness = fitness;
    }

    // 已验
    public double getFitness() {
        return this.fitness;
    }

    // 已验
    public String toString() {
        String output = "";
        for (int gene = 0; gene < this.chromosome.length; gene++) {
            output += this.chromosome[gene];
        }
        return output;
    }

    // 已验
    public Individual(int chromosomeLength) {
        int[] individual;
        individual = new int[chromosomeLength];
        for (int gene = 0; gene < chromosomeLength; gene++) {
            individual[gene] = gene;
        }
        this.chromosome = individual;
    }


    // 已验
    // 使用timetable初始化一个个体将特征加入染色体中
    public Individual(Timetable timetable) {
        // 获取班级的数目
        int numClasses = timetable.getNumClasses();
        // *3 一份基因班级 一份基因时间 一份基因教师
        int chromosomeLength = numClasses * 3;
        int newChromosome[] = new int[chromosomeLength];
        int chromosomeIndex = 0;
        for (Group group : timetable.getGroupsAsArray()) {
            for (int moduleId : group.getModuleIds()) {
                // 添加随机时间
                int timeslotId = timetable.getRandomTimeslot().getTimeslotId();
                newChromosome[chromosomeIndex] = timeslotId;
                chromosomeIndex++;
                // 添加随机教室
                int roomId = timetable.getRandomRoom().getRoomId();
                newChromosome[chromosomeIndex] = roomId;
                chromosomeIndex++;
                // 添加随机教师
                Module module = timetable.getModule(moduleId);
                newChromosome[chromosomeIndex] = module.
                        getRandomProfessorId();
                chromosomeIndex++;
            }
        }
        this.chromosome = newChromosome;
    }

    // 已验
    public boolean containsGene(int gene) {
        for (int i = 0; i < this.chromosome.length; i++) {
            if (this.chromosome[i] == gene) {
                return true;
            }
        }
        return false;
    }


    @Override
    public int hashCode(){
        int hash = Arrays.hashCode(this.chromosome);
        return hash;
    }

    @Override
    public boolean equals(Object obj){
        if(obj == null){
            return false;
        }
        if(getClass() != obj.getClass()){
            return false;
        }

        Individual individual = (Individual)obj;
        return  Arrays.equals(this.chromosome,individual.chromosome);
    }

}
